﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.FMCG.Buz.Interfaces;
using Microhard.Core.DBUtility;
using Microhard.FMCG.Buz.DataAccess;
using System.Data.Common;
using Microhard.Core;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;

namespace Microhard.FMCG.Buz.Impls
{
    public class OrderImpl : IOrder
    {

        public Core.DBUtility.ExecutedResult Add(Models.OrderModel model)
        {
            ExecutedResult result = new ExecutedResult();
            string addSql = "insert into order(Code,OrderDate,OrderStore,OrderTypeName,DeliveryAddress,Amount,CreateTime,Status,DeliveryDate,OrderType,SupplyCode,ViewDate " +
            ") values(@Code,@OrderDate,@OrderStore,@OrderTypeName,@DeliveryAddress,@Amount,@CreateTime,@Status,@DeliveryDate,@OrderType,@SupplyCode,@ViewDate)";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@Code", model.Code));
            parms.Add(new SqlParameter("@OrderDate", model.OrderDate));
            parms.Add(new SqlParameter("@OrderStore", model.OrderStore));
            parms.Add(new SqlParameter("@OrderTypeName", model.OrderTypeName));
            parms.Add(new SqlParameter("@DeliveryAddress", model.DeliveryAddress));
            parms.Add(new SqlParameter("@Amount", model.Amount));
            parms.Add(new SqlParameter("@Status", model.Status));
            parms.Add(new SqlParameter("@DeliveryDate", model.DeliveryDate));
            parms.Add(new SqlParameter("@CreateTime", DateTime.Now));
            parms.Add(new SqlParameter("@OrderType", string.Empty));
            parms.Add(new SqlParameter("@SupplyCode", string.Empty));
            parms.Add(new SqlParameter("@ViewDate", DateTime.Now));
            try
            {
                var rtn = SqlHelper.ExecuteNonQuery(DBHelper.GetCNN(),
                    System.Data.CommandType.Text,
                    addSql,
                    parms.ToArray());
            }
            catch (Exception ex)
            {
                Log.Error("OrderImpl.Add", ex);
                result.ErrorMsg.Add("Add", "添加订单失败");
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Update(Models.OrderModel model)
        {
            ExecutedResult result = new ExecutedResult();
            string addSql = "update order set ViewDate = @ViewDate,Status=@Status where ID = @ID";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@ViewDate", DateTime.Now));
            parms.Add(new SqlParameter("@Status", model.Status));
            parms.Add(new SqlParameter("@ID", model.OrderID));
            try
            {
                SqlHelper.ExecuteNonQuery(DBHelper.GetCNN(),
                    System.Data.CommandType.Text,
                    addSql,
                    parms.ToArray());
            }
            catch (Exception ex)
            {
                Log.Error("OrderImpl.Update", ex);
                result.ErrorMsg.Add("Update", "修改订单失败");
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ExecutedResult Delete(Models.OrderModel model)
        {
            ExecutedResult result = new ExecutedResult();
            //删除订单主数据
            string addSql = "delete from order where ID = @ID";
            List<SqlParameter> delParms = new List<SqlParameter>();
            delParms.Add(new SqlParameter("@ID", model.OrderID));
            //删除订单明细
            string addSqlDetail = "delete from OrderDetail where OrderID = @ID";
            List<SqlParameter> delParms2 = new List<SqlParameter>();
            delParms2.Add(new SqlParameter("@ID", model.OrderID));
            try
            {
                SqlHelper.ExecuteNonQuery(DBHelper.GetCNN(),
                     System.Data.CommandType.Text,
                     addSql,
                     delParms.ToArray());
                SqlHelper.ExecuteNonQuery(DBHelper.GetCNN(),
                     System.Data.CommandType.Text,
                     addSqlDetail,
                     delParms2.ToArray());
            }
            catch (Exception ex)
            {
                Log.Error("OrderImpl.Delete", ex);
                result.ErrorMsg.Add("Delete", "删除订单失败");
            }
            return result;
        }

        public Models.OrderModel GetModelByID(Guid id)
        {
            throw new NotImplementedException();
        }

        public Models.OrderModel GetModelByModel(Models.OrderModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.OrderModel> Query(Models.OrderModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            throw new NotImplementedException();
        }

        public List<Models.OrderModel> Query(Models.OrderModel Model)
        {
            List<Models.OrderModel> result = new List<Models.OrderModel>();
            string addSql = "select * from order where 1=1 ";
            List<SqlParameter> parms = new List<SqlParameter>();

            if (string.IsNullOrEmpty(Model.Code))
            {
                addSql += " and Code = @Code";
                parms.Add(new SqlParameter("@Code", Model.Code));
            }
            if (string.IsNullOrEmpty(Model.OrderTypeName))
            {
                addSql += " and OrderTypeName = @OrderTypeName";
                parms.Add(new SqlParameter("@OrderTypeName", Model.OrderTypeName));
            }
            if (Model.QueryStart <= Model.QueryEnd)
            {
                addSql += "  and OrderDate between @QueryStart and @QueryEnd ";
                parms.Add(new SqlParameter("@QueryStart", Model.QueryStart));
                parms.Add(new SqlParameter("@QueryEnd", Model.QueryEnd));
            }
            if (string.IsNullOrEmpty(Model.OrderStore))
            {
                addSql += "   and OrderStore = @OrderStore ";
                parms.Add(new SqlParameter("@OrderStore", Model.OrderStore));
            }
            if (Model.Status > 0)
            {
                addSql += "    and Status = @Status ";
                parms.Add(new SqlParameter("@Status", Model.Status));
            }
            try
            {
                var ds = SqlHelper.ExecuteDataset(DBHelper.GetCNN(),
                     System.Data.CommandType.Text,
                     addSql,
                     parms.ToArray());
                for (int n = 0; n < ds.Tables[0].Rows.Count; n++)
                {
                    result.Add(new Models.OrderModel
                    {
                        Amount = decimal.Parse(ds.Tables[0].Rows[n]["Amount"].ToString()),
                        Code = ds.Tables[0].Rows[n]["Code"].ToString(),
                        CreateTime = DateTime.Parse(ds.Tables[0].Rows[n]["CreateTime"].ToString()),
                        DeliveryAddress = ds.Tables[0].Rows[n]["DeliveryAddress"].ToString(),
                        DeliveryDate = DateTime.Parse(ds.Tables[0].Rows[n]["DeliveryDate"].ToString()),
                        OrderDate = DateTime.Parse(ds.Tables[0].Rows[n]["OrderDate"].ToString()),
                        OrderID = long.Parse(ds.Tables[0].Rows[n]["ID"].ToString()),
                        OrderStore = ds.Tables[0].Rows[n]["OrderStore"].ToString(),
                        OrderTypeName = ds.Tables[0].Rows[n]["OrderTypeName"].ToString(),
                        Status = int.Parse(ds.Tables[0].Rows[n]["Status"].ToString())
                    });
                }
            }
            catch (Exception ex)
            {
                Log.Error("OrderImpl.Query", ex);
            }
            return result;
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }

        public bool IsExist(Models.OrderModel model)
        {
            string addSql = "select count(*) from order where ID = @ID";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@ID", model.OrderID));
            var obj = SqlHelper.ExecuteScalar(DBHelper.GetCNN(),
                     System.Data.CommandType.Text,
                     addSql,
                     parms.ToArray());
            if (obj != null)
            {
                int cnt = int.Parse(obj.ToString());
                return cnt > 0 ? true : false;
            }
            return false;
        }
    }
}
